﻿using System;
using System.ComponentModel;
using System.Diagnostics.Contracts;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using OpenWaves.Web;
using System.Linq;

namespace OpenWaves.ActivityFeed.Web.Controls
{
    [DefaultProperty("Text")]
    [ToolboxData("<{0}:ActivityFeed runat=\"server\"></{0}:ActivityFeed>")]
    public class ActivityFeed : ActivityFeedBase
    {
        public ActivityFeed() : base()
        {
        }

        protected override void OnInit(EventArgs e)
        {
            var content = this.Page.LoadControl(AssemblyResourceVirtualPathProvider.CreateUrl(typeof(ActivityFeedDefaultTemplate)).ToString()) as ActivityFeedDefaultTemplate;
            Contract.Assert(content != null);

            if (this.Template == null)
                this.Template = content.Template;

            if ( this.UpdateTemplate == null)
                this.UpdateTemplate = content.UpdateTemplate;

            if (this.CommentTemplate == null)
                this.CommentTemplate = content.CommentTemplate;

            Contract.Assert(this.UpdateContentTemplates.Select(t => t.ContentType).Distinct().Count() == this.UpdateContentTemplates.Count(), "List must contain unique content types.");

            if (this.UpdateContentTemplates.Any(t=>t.ContentType == content.PostTemplate.ContentType) == false)
                this.UpdateContentTemplates.Add(content.PostTemplate);
        }
    }
}

